const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop_id:'',
    num:1,
    items: [
      { name: 'week_0', value: '星期日','checked':''},
      { name: 'week_1', value: '星期一'},
      { name: 'week_2', value: '星期二' },
      { name: 'week_3', value: '星期三' },
      { name: 'week_4', value: '星期四' },
      { name: 'week_5', value: '星期五' },
      { name: 'week_6', value: '星期六' },
    ],
    value:{num: 1, yuyue: true },
  },
  backto: function (e) {
    wx.navigateBack({})
  },
  numchange:function(e){
    var that = this;
    var a = e.detail.value;
    var b = that.data.value;
     b.num = a;
    that.setData({
      num: a,
      value:b
    });
  },
  onLoad:function(options){
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid
    })
    var that = this;
    that.setData({
      shop_id:options.shop_id
    });
    app.ihttpGet('https://www.58zltc.com/api.php/Shop/getCardInfo/shop_id/' + that.data.shop_id, function (resq) {
      if (resq.data) {
        //周几信息
        var weekArr = ['week_0', 'week_1', 'week_2', 'week_3', 'week_4', 'week_5', 'week_6',];
        var weekInfo = resq.data;
        var ite = that.data.items;
        var values = resq.data;
        var valueWeek = [];

        for (var i = 0; i < ite.length; i++) {
          console.log(weekInfo[weekArr[i]] + '--')
          if (weekInfo[weekArr[i]] == 1) {
            ite[i].checked = 'checked';
            valueWeek.push(weekArr[i]);
          }
        }
        //特权日栏目
        var TVal;
        weekInfo.is_day == 1 ? TVal = 1 : '';
        weekInfo.is_week == 1 ? TVal = 2 : '';
        values.day = valueWeek;
        values.yuyue = resq.data.yuyue == 1 ? true : false;
        that.setData({
          shopCardInfo: resq.data,
          items: ite,
          num: TVal,
          value: values
        })
      }
    })
  },
  checkboxChange: function (e) {
    var that=this;
    var a = that.data.value;
    a.day = e.detail.value;
    that.setData({
      value: a
    });
  },
  inputchange: function (e) {
    var that = this;
    var a = that.data.value;
    a.days = e.detail.value;
    that.setData({
      value: a
    });
  },
  switch1Change:function(e){
    var that = this;
    var a = that.data.value;
    a.yuyue = e.detail.value;
    that.setData({
      value: a
    });
  },
  radioChange: function (e) {
    var that = this;
    var a = that.data.value;
    a.yuyue = e.detail.value;
    that.setData({
      value: a
    });
  },
  youhuiChange: function (e) {
    var that = this;
    var a = that.data.value;
    a.discount = e.detail.value;
    that.setData({
      value: a
    });
  },
  youhuiRemarkChange: function (e) {
    var that = this;
    var a = that.data.value;
    a.discount_remark = e.detail.value;
    that.setData({
      value: a
    });
  },
  countChange: function (e) {
    var that = this;
    var a = that.data.value;
    a.count = e.detail.value;
    that.setData({
      value: a
    });
  },
  frequencyChange: function (e) {
    var that = this;
    var a = that.data.value;
    a.frequency = e.detail.value;
    that.setData({
      value: a
    });
  },
  fenxiaoChange: function (e) {
    var that = this;
    var a = that.data.value;
    a.fenxiao = e.detail.value;
    that.setData({
      value: a
    });
  },
  bindTextAreaBlur: function (e) {
    var that = this;
    var a = that.data.value;
    a.shuoming = e.detail.value;
    that.setData({
      value: a
    });
  },
  submit: function () {
    var that = this;
    var value = that.data.value;
    var shop_id = that.data.shop_id;
    //
    var post_data = value;
    post_data.shop_id = shop_id
    post_data.num = that.data.num
    console.log(post_data)
    app.ihttpPost('https://www.58zltc.com/api.php/Shop/openShopcard', post_data, function (res) {
      var res = res.data
      if(res.err_code == 400){
        wx.showToast({
          icon:'none',
          title: res.err_msg,
        })
      }else{
        wx.showToast({
          title: res.err_msg,
        })
        setTimeout(function(){
          wx.navigateBack({})
        },1500)
      }
    })
  },
})